/**
 * Title:        系统默认工作流检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.io.File;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.json.JSONObject;

import weaver.conn.RecordSetTrans;
import weaver.general.GCONST;
import weaver.general.Util;



public class SysDefaultWorkflowCheck extends BaseBean implements CheckBase  {

    private List<String> statictablenameList = Arrays.asList("workflow_formfield",
            "workflow_nodeform",
            "workflow_fieldlable",
            "workflow_flownode",
            "workflow_formbase",
            "workflow_groupdetail",
            "workflow_nodebase",
            "workflow_nodegroup",
            "workflow_nodelink",
            "workflow_base",
            "workflow_type");
    private List<String> staticsqlwhereList = Arrays.asList("formid=14",
            "nodeid in(1,2,3)",
            "formid=14",
            "workflowid=1",
            "id=14",
            "id in(1,2)",
            "id in(1,2,3)",
            "id in(1,2)",
            "id in(1,2)",
            "id=1",
            "id=1");

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("SysDefaultWorkflowCheck  start");

        String result = resultOk;
        List resultList = new ArrayList();
        for(int i = 0; i < statictablenameList.size(); i++){
            String tablename = statictablenameList.get(i);
            String sqlwhere = staticsqlwhereList.get(i);
            Map resultMap = checkSql(tablename, sqlwhere);
            if(resultMap != null) resultList.add(resultMap);
        }
        if(resultList.size() <= 0 ){
            result = resultOk;
            resultList.add("系统默认工作流数据正常");
        }else{
            result = resultNeedFix;
        }
        
        (new BaseBean()).writeLog("SysDefaultWorkflowCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }
    private Map checkSql(String tablename, String sqlWhere){
        Map resultMap = new HashMap<String,String>();
    
        String sqlStr = "";
        sqlStr=" select * from " + tablename + " where " + sqlWhere;
        recordSet.executeSql(sqlStr);
        if(recordSet.getCounts() == 0){
            resultMap.put("showMessage",formatMessage("数据表", "没有数据。", tablename));
            resultMap.put("tablename",tablename);
            resultMap.put("buttonname","修复");
        }else{
            return null;
        }
        return resultMap;
    }
    @Override
    public JSONObject fixData(HashMap dataMap) {

        setRecordSet();
        (new BaseBean()).writeLog("WorkflowCodeCheck  start");

        List resultList = new ArrayList();
        String result = resultOk;
        if(dataMap.get("tablename") != null && !dataMap.get("tablename").equals("")){
            //根据参数获取列表
            String tablenames = (String)dataMap.get("tablename");
            String divids =  (String)dataMap.get("divid");
            List<String> tablenameList = Util.splitString2List(tablenames, ",");
            List<String> dividList = Util.splitString2List(divids, ",");
            updatesqlByTablename(tablenameList, dividList);
            //return formatReturnJsonFix(result, resultList,this);
        }else{
            updatesqlByTablename(statictablenameList, null);
        }
        needReload = "1";
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "系统默认工作流检测";
    }

    @Override
    public String getClassDesc() {
        // TODO Auto-generated method stub
        return "检测系统默认工作流数据信息是否完整";
    }
    private void updatesqlByTablename(List<String> tablenameList,List<String> dividList){

        Connection jdbc_conn=null;
        String driverClasses = getPropValue(GCONST.getConfigFile() , "DriverClasses");
        String poolName = GCONST.getServerName();
        String url = getPropValue(GCONST.getConfigFile() , poolName + ".url");                              //链接的url
        String user = getPropValue(GCONST.getConfigFile() , poolName + ".user");                               //链接的用户名
        String password = getPropValue(GCONST.getConfigFile() , poolName + ".password");                       //链接的密码
        boolean isoracle = (recordSet.getDBType()).equals("oracle") ;
        boolean isdb2 = (recordSet.getDBType()).equals("db2") ;
        String sqlEndFlag = "";
        try {
            // 获得需要升级的数据库脚本目录和系统原有的数据库脚本目录
            if( isoracle ) {
                sqlEndFlag = "  " ;
            }
            else if(isdb2) {
                sqlEndFlag = "  " ;
            }
            else {
                sqlEndFlag = "  " ;
            }
            Driver driver = (Driver)Class.forName(driverClasses).newInstance();
            DriverManager.registerDriver(driver);
            Properties props = new Properties();
            props.put("user", user);
            props.put("password", password);
            props.put("CHARSET", "ISO");
            jdbc_conn = DriverManager.getConnection(url, props);

            Statement statement=jdbc_conn.createStatement();
            for(int i = 0; i < tablenameList.size(); i++){
                String tablename = tablenameList.get(i);
                int index = staticsqlwhereList.indexOf(tablename);
                if(index < 0){
                    continue;
                }
                String sqlWhere = staticsqlwhereList.get(i);
                String divid = "";
                if(dividList != null && dividList.size() > i){
                    divid = dividList.get(i);
                }
                String sqlStr=" select 1 from " + tablename + " where " + sqlWhere;
                recordSet.executeUpdate(sqlStr);
                if(recordSet.next()){
                    continue;
                }
                String sqlstr = "";
                if("workflow_formfield".equals(tablename)){
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,1,'','','','',0)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,2,'','','','',1)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,3,'','','','',2)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,4,'','','','',3)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,5,'','','','',4)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,18,'','','','',5)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_formfield (formid,fieldid,fieldparameter,needcheck,checkscript,ismultirows,fieldorder) values (14,6,'','','','',6)";
                    recordSet.executeUpdate(sqlstr);
                }
                if("workflow_nodeform".equals(tablename)){
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,1,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,2,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,3,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,4,'0','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,5,'0','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,18,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (1,6,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,1,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,2,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,3,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,4,'0','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,5,'0','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,18,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (2,6,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,1,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,2,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,3,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,4,'0','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,5,'0','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,18,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodeform (nodeid,fieldid,isview,isedit,ismandatory) values (3,6,'1','0','0')";
                    recordSet.executeUpdate(sqlstr);
                }
                if("workflow_fieldlable".equals(tablename)){
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,1,'相关文档',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,2,'相关客户',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,3,'相关项目',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,4,'执行者',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,5,'提醒者',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,18,'相关会议',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_fieldlable (formid,fieldid,fieldlable,langurageid,isdefault) values (14,6,'备注',7,'1')";
                    recordSet.executeUpdate(sqlstr);
                }
                if("workflow_flownode".equals(tablename)){
                    sqlstr = "insert into workflow_flownode (workflowid,nodeid,nodetype) values (1,1,'0')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_flownode (workflowid,nodeid,nodetype) values (1,2,'2')";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_flownode (workflowid,nodeid,nodetype) values (1,3,'3')";
                    recordSet.executeUpdate(sqlstr);
                }
                if("workflow_nodegroup".equals(tablename)){
                    sqlstr = "insert into workflow_nodegroup (id,nodeid,groupname,canview) values (1,2,'提醒',1)";
                    recordSet.executeUpdate(sqlstr);
                    sqlstr = "insert into workflow_nodegroup (id,nodeid,groupname,canview) values (2,3,'执行',1)";
                    recordSet.executeUpdate(sqlstr);
                }
                if("workflow_formbase".equals(tablename)){
                    if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                        sqlstr = "insert into workflow_formbase (id,formname,formdesc,securelevel,formhtmlcode,formdate) values (14,'系统默认提醒表单','系统默认提醒表单','','','')";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_formbase set id=14 where id=(select max(id) from workflow_formbase);";
                        recordSet.executeUpdate(sqlstr);
                    }else{
                        sqlstr = " SET identity_insert workflow_formbase ON";
                        sqlstr += sqlEndFlag;
                        sqlstr += " insert into workflow_formbase (id,formname,formdesc,securelevel,formhtmlcode,formdate) values (14,'系统默认提醒表单','系统默认提醒表单','','','')";
                        sqlstr += sqlEndFlag;
                        sqlstr += " SET identity_insert workflow_formbase OFF";
                        statement.execute(sqlstr);
                    }
                }
                if("workflow_groupdetail".equals(tablename)){

                    if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                        sqlstr = "insert into workflow_groupdetail (id,groupid,type,objid,level_n) values (1,1,5,5,1)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_groupdetail set id=1 where id=(select max(id) from workflow_groupdetail)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "insert into workflow_groupdetail (id,groupid,type,objid,level_n) values (2,2,5,4,0)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_groupdetail set id=2 where id=(select max(id) from workflow_groupdetail)";
                        recordSet.executeUpdate(sqlstr);
                    }else{
                        sqlstr = "SET identity_insert workflow_groupdetail ON ";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_groupdetail (id,groupid,type,objid,level_n) values (1,1,5,5,1)";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_groupdetail (id,groupid,type,objid,level_n) values (2,2,5,4,0)";
                        sqlstr += sqlEndFlag;
                        sqlstr = "SET identity_insert workflow_groupdetail OFF";
                        statement.executeUpdate(sqlstr);
                    }

                }
                if("workflow_nodebase".equals(tablename)){
                    if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                        sqlstr = "insert into workflow_nodebase (id,nodename,isstart,isreject,isreopen,isend,drawxpos,drawypos) values (1,'系统创建','1','0','0','0',70,66)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_nodebase set id=1 where id=(select max(id) from workflow_nodebase);";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "insert into workflow_nodebase (id,nodename,isstart,isreject,isreopen,isend,drawxpos,drawypos) values (2,'提醒','0','0','1','0',230,66)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_nodebase set id=2 where id=(select max(id) from workflow_nodebase)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "insert into workflow_nodebase (id,nodename,isstart,isreject,isreopen,isend,drawxpos,drawypos) values (3,'执行','0','0','0','1',391,66)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_nodebase set id=3 where id=(select max(id) from workflow_nodebase)";
                        recordSet.executeUpdate(sqlstr);
                    }else{
                        sqlstr = "SET identity_insert workflow_nodebase ON";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_nodebase (id,nodename,isstart,isreject,isreopen,isend,drawxpos,drawypos) values (1,'系统创建','1','0','0','0',70,66)";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_nodebase (id,nodename,isstart,isreject,isreopen,isend,drawxpos,drawypos) values (2,'提醒','0','0','1','0',230,66)";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_nodebase (id,nodename,isstart,isreject,isreopen,isend,drawxpos,drawypos) values (3,'执行','0','0','0','1',391,66)";
                        sqlstr += sqlEndFlag;
                        sqlstr += "SET identity_insert workflow_nodebase OFF";
                        statement.executeUpdate(sqlstr);
                    }
                }
                if("workflow_nodebase".equals(tablename)){
                    if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                        sqlstr = "insert into workflow_nodelink (id,workflowid,nodeid,isreject,condition,linkname,destnodeid,directionfrom,directionto,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,nodepasstime) values (1,1,1,' ','','提醒',2,0,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_nodelink set id=1 where id=(select max(id) from workflow_nodelink)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "insert into workflow_nodelink (id,workflowid,nodeid,isreject,condition,linkname,destnodeid,directionfrom,directionto,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,nodepasstime) values (2,1,2,' ','','执行',3,0,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_nodelink set id=2 where id=(select max(id) from workflow_nodelink)";
                        recordSet.executeUpdate(sqlstr);
                    }else{
                        sqlstr = "SET identity_insert workflow_nodelink ON";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_nodelink (id,workflowid,nodeid,isreject,condition,linkname,destnodeid,directionfrom,directionto,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,nodepasstime) values (1,1,1,' ','','提醒',2,0,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_nodelink (id,workflowid,nodeid,isreject,condition,linkname,destnodeid,directionfrom,directionto,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,nodepasstime) values (2,1,2,' ','','执行',3,0,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)";
                        sqlstr += sqlEndFlag;
                        sqlstr += "SET identity_insert workflow_nodelink OFF";
                        statement.executeUpdate(sqlstr);
                    }
                }
                if("workflow_base".equals(tablename)){
                    if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                        sqlstr = "insert into workflow_base (id,workflowname,workflowdesc,workflowtype,securelevel,formid,isbill,iscust,helpdocid,isvalid,needmark) values (1,'系统提醒工作流','系统自动触发的提醒工作流程',1,'',14,'0',0,0,'1',' ')";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_base set id=1 where id=(select max(id) from workflow_base)";
                        recordSet.executeUpdate(sqlstr);
                    }else{

                        sqlstr = "SET identity_insert workflow_base ON ";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_base (id,workflowname,workflowdesc,workflowtype,securelevel,formid,isbill,iscust,helpdocid,isvalid,needmark) values (1,'系统提醒工作流','系统自动触发的提醒工作流程',1,'',14,'0',0,0,'1',' ') ";
                        sqlstr += sqlEndFlag;
                        sqlstr += "SET identity_insert workflow_base OFF";
                        statement.executeUpdate(sqlstr);
                    }
                }
                if("workflow_type".equals(tablename)){
                    if(recordSet.getDBType().equalsIgnoreCase("oracle")){
                        sqlstr = "insert into workflow_type (ID,TYPENAME,TYPEDESC,DSPORDER) values (1,'系统默认工作流','系统默认工作流',-99)";
                        recordSet.executeUpdate(sqlstr);
                        sqlstr = "update workflow_type set id=1 where id=(select max(id) from workflow_type)";
                        recordSet.executeUpdate(sqlstr);
                    }else{

                        sqlstr = "SET identity_insert workflow_type ON ";
                        sqlstr += sqlEndFlag;
                        sqlstr += "insert into workflow_type (ID,TYPENAME,TYPEDESC,DSPORDER) values (1,'系统默认工作流','系统默认工作流',-99) ";
                        sqlstr += sqlEndFlag;
                        sqlstr += "SET identity_insert workflow_type OFF";
                        statement.executeUpdate(sqlstr);
                    }
                }
                sqlstr = "update workflow_requestbase set workflowid=1 ,lastnodeid=1,currentnodeid=1 where workflowid=0";
                recordSet.executeUpdate(sqlstr);
                sqlstr = "update workflow_currentoperator set workflowid=1,workflowtype=1,nodeid=1 where workflowid=0";
                recordSet.executeUpdate(sqlstr);
                sqlstr = "update workflow_requestLog set workflowid=1, nodeid=1 where workflowid = 0";
                recordSet.executeUpdate(sqlstr);
                sqlstr = "update workflow_form set billformid=14 where billformid=0";
                recordSet.executeUpdate(sqlstr);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}



